package cn.scw.common;

import cn.scw.common.constant.Constants;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.lowagie.text.pdf.PdfReader;
import com.spire.doc.Document;
import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class PoiDoc {

    public static void main(String[] args) throws IOException {
        String filePath = "D:/111111.xlsx";// 8
//        String filePath = "D:/爱尔脑数据库文档.docx";// 8

//        com.spire.xls.Workbook workbook = new com.spire.xls.Workbook();
//        workbook.loadFromFile(filePath);

        System.out.println(getFileName("/home/scw_print/uploadPath/upload/2020/11/10/3de34fd3428840178fe956b5dfddb676.xls"));
    }

    public static int getCount(String netPath) {
        String localpath = netPath2localPath(netPath);
        if (localpath.endsWith(".xls") || localpath.endsWith(".xlsx")) {
            localpath = excelToPdf(localpath);
        }
        return readWordPage(localpath);
    }

    /**
     * excel转pdf
     *
     * @param localpath excel 本地地址
     * @return
     */
    public static String excelToPdf(String localpath) {
        String pdfFile = Constants.UPLOAD_FILE + "/excelToPdf/" + getFileName(localpath) + ".pdf";
        File file = new File(pdfFile);
        if (!file.exists()) {
            Excel2PdfUtil.excel2Pdf(localpath, pdfFile);
        }
        return pdfFile;
    }


    /**
     * 获取文件名
     *
     * @param localpath
     * @return
     */
    public static String getFileName(String localpath) {
        return localpath.substring(localpath.lastIndexOf("/") + 1, localpath.lastIndexOf("."));
    }

    /***
     * 网络逻辑转本地路径
     * @param netPath
     * @return
     */
    public static String netPath2localPath(String netPath) {
        String localpath = netPath;
        int index = netPath.indexOf("upload");
        if (index > 0) {
            localpath = netPath.substring(index);
        } else {
            index = netPath.indexOf("profile/");
            if (index > 0) {
                localpath = netPath.substring(index + 8);
            }
        }
        localpath = Constants.UPLOAD_FILE + "/" + localpath;
        return localpath;
    }

    public static int pdfPage(String path) throws IOException {
        int page = 0;
        PdfReader pdfReader = new PdfReader(new FileInputStream(path));
        page = pdfReader.getNumberOfPages();
        return page;
    }

    /**
     * 计算文件页数
     *
     * @param filePath
     * @return
     */
    public static int readWordPage(String filePath) {
        try {

            String lowerFilePath = filePath.toLowerCase();
            if (lowerFilePath.endsWith(".xls")) {
                HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(lowerFilePath));
                Integer sheetNums = workbook.getNumberOfSheets();
                if (sheetNums > 0) {
                    return workbook.getSheetAt(0).getRowBreaks().length + 1;
                }
            } else if (lowerFilePath.endsWith(".xlsx")) {
                XSSFWorkbook xwb = new XSSFWorkbook(lowerFilePath);
                Integer sheetNums = xwb.getNumberOfSheets();
                if (sheetNums > 0) {
                    return xwb.getSheetAt(0).getRowBreaks().length + 1;
                }
            } /*else if (lowerFilePath.endsWith(".docx") || lowerFilePath.endsWith(".doc")) {
                Document document = new Document(filePath);
                return document.getPageCountEx();
            }*/
            else if (lowerFilePath.endsWith(".docx")) {
                XWPFDocument docx = new XWPFDocument(POIXMLDocument.openPackage(lowerFilePath));
                return docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();

            } else if (lowerFilePath.endsWith(".doc")) {
                Document document = new Document(filePath);
                return document.getPageCountEx();

            }
            else if (lowerFilePath.endsWith(".ppt")) {
                HSLFSlideShow slideShow = new HSLFSlideShow(new FileInputStream(lowerFilePath));
                return slideShow.getSlides().size();
            } else if (lowerFilePath.endsWith(".pptx")) {
                XMLSlideShow xslideShow = new XMLSlideShow(new FileInputStream(lowerFilePath));
                return xslideShow.getSlides().size();
            } else if (lowerFilePath.endsWith(".pdf")) {
                PDFParser parser = new PDFParser(new RandomAccessBuffer(new FileInputStream(filePath)));
                parser.parse();
                // 读取文本内容
                PDDocument document = parser.getPDDocument();
                return document.getNumberOfPages();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

}
